﻿using System;
using System.Collections.Generic;
using System.Text;
using ULTCONFIGURATIONLib;
using Portal.Interface;
using Portal.Entity;
using System.Data;
using MyLib;
using System.Web;

namespace Workflow.Logic
{
    public class UltimusOrgLogic:IOrg,IResource
    {
        public virtual bool CheckUser(string loginName, string password)
        {
            Ultimus.OC.OrgChart oc = new Ultimus.OC.OrgChart();
            bool flag = oc.VerifyUser(loginName.Trim(), password);
            return flag;
        }

        static List<string> _domains = new List<string>();
        public virtual List<string> GetDomains()
        {
            if (_domains.Count > 0)
            {
                return _domains;
            }
            byte num1;
            string[] array1;
            int num2;
            Array array2;
            int num3;
            num1 = 0;
            array1 = null;
            num2 = 0;

            ConfigureClass oConfig = new ConfigureClass();
            array2 = ((Array)oConfig.GetDomains(num1, num2));
            if (array2.Length > 0)
            {
                array1 = new string[array2.Length];
                for (num3 = 0; (num3 < array2.Length); num3 = (num3 + 1))
                {
                    array1[num3] = array2.GetValue(num3).ToString();

                }
            }
            _domains.AddRange(array1);
            return _domains; ;
        }

        static List<ResourceEntity> _list = null;
        public virtual List<Portal.Entity.ResourceEntity> GetOrgList()
        {
            if (_list == null)
            {
                List<ResourceEntity> list = new List<ResourceEntity>();
                DataTable dt = DataAccess.Instance("UltDB").ExecuteDataTable("SELECT * FROM DEPARTMENTS");
                foreach (DataRow row in dt.Rows)
                {
                    ResourceEntity ety = new ResourceEntity();
                    ety.CNNAME = ConvertUtil.ToString(row["NAME"]);
                    ety.ENNAME = ConvertUtil.ToString(row["NAME"]);
                    ety.RESOURCEID = ConvertUtil.ToInt32(row["ID"]);
                    ety.PARENTID = ConvertUtil.ToInt32(row["PARENTDEPT"]);
                    ety.TYPE = "Org";
                    list.Add(ety);
                }

                _list = list;
            }
            return _list;
        }

        public virtual List<Portal.Entity.ResourceEntity> FindOrg(string orgName)
        {
            List<ResourceEntity> list = GetOrgList();
            return list.FindAll(p => p.CNNAME.IndexOf(orgName) >= 0);
        }

        public virtual List<Portal.Entity.UserEntity> GetUserList(string orgId)
        {
            DataTable dt = DataAccess.Instance("UltDB").ExecuteDataTable("SELECT * FROM JOBS WHERE LEN(NAME)>0 AND DEPTID=" + orgId);
            List<UserEntity> list = new List<UserEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                UserEntity user = new UserEntity();
                user.CNName = ConvertUtil.ToString(dr["FullName"]);
                user.ENName = ConvertUtil.ToString(dr["FullName"]);
                user.LoginName = ConvertUtil.ToString(dr["Name"]);
                user.JobFunction = ConvertUtil.ToString(dr["JobFunction"]);
                user.UserId = ConvertUtil.ToInt32(dr["ID"]);
                list.Add(user);
            }

            return list;
        }

        public virtual Portal.Entity.UserEntity GetUserEntity(string loginName)
        {
            if (HttpContext.Current.Session["UserEntity"] != null)
            {
                return HttpContext.Current.Session["UserEntity"] as Portal.Entity.UserEntity;
            }
            UserEntity ety = new UserEntity();
            Ultimus.OC.OrgChart oc = new Ultimus.OC.OrgChart();
            Ultimus.OC.User user = new Ultimus.OC.User();
            oc.FindUser(loginName.Replace("\\","/"), "", 0, out user);

            ety.LoginName = loginName;
            ety.CNName = user.strUserFullName;
            ety.ENName = user.strUserFullName;
            ety.UserId = user.nUserID;
            ety.JobFunction = user.strJobFunction;
            ety.Ext01 = user.strDepartmentName; //ext01:当前部门名称
            //Ultimus.OC.Department[] depts;
            //user.GetUserDepartments(out depts);
            //if (depts != null && depts.Length > 0)
            //{
            //    ety.Ext02 = depts[0].strDepartmentID; //ext02:当前部门ID
            //}            
            HttpContext.Current.Session["UserEntity"] = ety;
            return ety;
        }

        public virtual List<Portal.Entity.UserEntity> GetUserList(string orgId, string jobFunction)
        {
            List<UserEntity> list = GetUserList(orgId);
            return list.FindAll(p => p.JobFunction.Trim().ToUpper() == jobFunction.ToUpper().Trim());
        }

        public virtual List<Portal.Entity.UserEntity> FindUser(string loginName, string name)
        {
            DataTable dt = DataAccess.Instance("UltDB").ExecuteDataTable("SELECT * FROM JOBS WHERE name like '%" + loginName + "%' or fullname like '%" + name + "%'");
            List<UserEntity> list = new List<UserEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                UserEntity user = new UserEntity();
                user.CNName = ConvertUtil.ToString(dr["FullName"]);
                user.ENName = ConvertUtil.ToString(dr["FullName"]);
                user.LoginName = ConvertUtil.ToString(dr["Name"]);
                user.JobFunction = ConvertUtil.ToString(dr["JobFunction"]);
                user.UserId = ConvertUtil.ToInt32(dr["ID"]);
                list.Add(user);
            }

            return list;
        }

        public virtual List<ResourceEntity> GetResourceList()
        {
            return GetOrgList();
        }

        public void ClearResourceList()
        {
            _list = new List<ResourceEntity>();
        }
    }
}
